/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.EstablishmentCommunicateBean;
import com.minierp.model.EstablishmentCommunicate;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class EstablishmentCommunicateValidator implements Validator {
	public boolean supports(Class clazz) {
		return EstablishmentCommunicateBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		EstablishmentCommunicateBean bean = (EstablishmentCommunicateBean) obj;
		EstablishmentCommunicate form = bean.getCommunicate();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		boolean hasError = false;
		if (!hasError
				&& (form.getTitle() == null || form.getTitle().trim()
						.equals(""))) {
			errors.rejectValue("communicate.title", "", "请输入『主题』");
			hasError = true;
		}

		if (!hasError) {
			try {
				form.setCommunicateTime(sf.parse(bean.getCommunicateTimeStr()
						+ " " + bean.getCommunicateTimeHour() + ":"
						+ bean.getCommunicateTimeMinute() + ":00"));
			} catch (Exception arg8) {
				hasError = true;
				errors.rejectValue("communicateTimeStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getContent() != null
					&& !form.getContent().trim().equals("")) {
				if (form.getContent().length() >= 500) {
					errors.rejectValue("communicate.content", "",
							"『内容』不能超过500个字");
					hasError = true;
				}
			} else {
				errors.rejectValue("communicate.content", "", "请输入『内容』");
				hasError = true;
			}
		}

		if (!hasError) {
			if (form.getCommType().equals("PLAN")) {
				if (form.getSmsRemind().equals("Y")) {
					try {
						form.setSmsRemindDate(sf.parse(bean.getSrDateStr()
								+ " " + bean.getSrDateHour() + ":"
								+ bean.getSrDateMinute() + ":00"));
						if (!bean.getOldSrDate().equals(
								bean.getSrDateStr() + " "
										+ bean.getSrDateHour() + ":"
										+ bean.getSrDateMinute())) {
							form.setSendSms("Y");
							if (form.getSmsRemindDate().before(new Date())) {
								hasError = true;
								errors.rejectValue("srDateStr", "",
										"『定时提醒时间』不能小于当前时间");
							}
						}
					} catch (Exception arg7) {
						hasError = true;
						errors.rejectValue("srDateStr", "",
								"『定时提醒时间』填写错误，请按以下格式输入：2008-01-01");
					}
				} else {
					form.setSmsRemindDate((Date) null);
					form.setSendSms("N");
				}
			} else {
				form.setForAll("N");
				form.setSmsRemind("N");
				form.setSmsRemindDate((Date) null);
				form.setSendSms("N");
			}
		}

	}
}